package com.info33.platform.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.exception.TransactionalException;
import com.info33.platform.common.mvc.entity.BaseEntity;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.dao.SysDeptPostDao;
import com.info33.platform.system.dao.SysMenuUserDao;
import com.info33.platform.system.dao.SysPostDao;
import com.info33.platform.system.entity.SysDeptPost;
import com.info33.platform.system.entity.SysMenuUser;
import com.info33.platform.system.entity.SysPost;
import com.info33.platform.system.service.SysPostService;
import com.info33.platform.system.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author admin
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostDao, SysPost> implements SysPostService {

    @Autowired
    private SysPostDao sysPostDao;
    @Autowired
    private SysDeptPostDao sysDeptPostDao;
    @Autowired
    private SysMenuUserDao sysMenuUserDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result<Object> removeList(List<Long> ids, SysUserVO currentUser) {
        for (Long id : ids) {
            SysPost entity = sysPostDao.selectById(id);
            if (ObjectUtils.isEmpty(entity)) {
                return Result.error(id + " 数据不存在");
            }
            // 存在该岗位的用户，不能删除
            if (sysDeptPostDao.selectCount(new QueryWrapper<SysDeptPost>().eq("post_id", id)) > 0) {
                return Result.error("有用户绑定在此岗位 '" + entity.getName() + "'，不能删除");
            }
            // 删除岗位分配的菜单
            List<Long> oldPostIdList = sysMenuUserDao
                    .selectList(new QueryWrapper<SysMenuUser>()
                            .select(TableConstant.ID)
                            .eq("post_id", id)).stream().map(BaseEntity::getId).collect(Collectors.toList());
            if (!ObjectUtils.isEmpty(oldPostIdList) && sysMenuUserDao.deleteBatchIds(oldPostIdList) != oldPostIdList.size()) {
                throw new TransactionalException();
            }
        }
        if (sysPostDao.deleteBatchIds(ids) != ids.size()) {
            throw new TransactionalException();
        }
        return Result.ok();
    }

    @Override
    public Result<Object> pcAdd(SysPost entity, SysUserVO currentUser) {
        // 角色名称不能重复
        SysPost role = sysPostDao.selectOne(new QueryWrapper<SysPost>()
                .eq(TableConstant.TENANT_ID, currentUser.getSysTenantUserVO().getTenantId())
                .eq("name", entity.getName())
                .last(TableConstant.LIMIT_ONE));
        if (!ObjectUtils.isEmpty(role)) {
            return Result.error("岗位名称已存在");
        }
        return sysPostDao.insert(entity) > 0 ? Result.ok(entity) : Result.error();
    }

    @Override
    public Result<Object> pcEdit(SysPost entity, SysUserVO currentUser) {
        // 角色名称不能重复
        SysPost old = sysPostDao.selectById(entity.getId());
        if (!old.getName().equals(entity.getName())) {
            SysPost role = sysPostDao.selectOne(new QueryWrapper<SysPost>()
                    .eq(TableConstant.TENANT_ID, currentUser.getSysTenantUserVO().getTenantId())
                    .eq("name", entity.getName())
                    .last(TableConstant.LIMIT_ONE));
            if (!ObjectUtils.isEmpty(role)) {
                return Result.error("岗位名称：" + entity.getName() + "，已存在");
            }
        }
        return sysPostDao.updateById(entity) > 0 ? Result.ok(entity) : Result.error();
    }
}